package com.example.huixue.controller;

import com.example.huixue.common.Result;
import com.example.huixue.service.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
public class CategoryController {
    
    private static final Logger logger = LoggerFactory.getLogger(CategoryController.class);
    
    @Autowired
    private CategoryService categoryService;
    
    /**
     * 获取所有分类
     */
    @GetMapping("/categories")
    public Result<?> getAllCategories() {
        try {
            logger.info("获取所有分类请求");
            return Result.success("获取分类成功", categoryService.getAllCategories());
        } catch (Exception e) {
            logger.error("获取分类失败", e);
            return Result.error("获取分类失败：" + e.getMessage());
        }
    }
    
    /**
     * 管理员获取所有分类 (用于管理页面)
     */
    @GetMapping("/api/categories")
    public Result<?> getAdminCategories() {
        try {
            logger.info("管理员获取所有分类请求");
            return Result.success("获取分类成功", categoryService.getAllCategories());
        } catch (Exception e) {
            logger.error("管理员获取分类失败", e);
            return Result.error("获取分类失败：" + e.getMessage());
        }
    }
} 